package com.qf;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Mail_Demo3 {
    public static void main(String[] args) {
        SendEmailInfoUser3("激活此邮件","邮件内容","50770284@qq.com","50770284@qq.com");
    }
    private static String sender = "mueryx@qq.com";
    private static String password = "iufmwkzerisrbcbf";
    private static String emailServer = "smtp.qq.com";
    private static String protocol = "smtp";

    public static void SendEmailInfoUser3(String title, String content, String copyReceiverAddress,String receiver) {
        Properties properties = new Properties();
        //设置服务器名称
        properties.setProperty("mail.host", emailServer);
        //设置邮件传输协议
        properties.setProperty("mail.transport.protocol", protocol);
        //设置是否要验证服务器用户名和密码
        properties.setProperty("mail.smtp.auth", "true");
        // 1.创建客户端与邮箱服务器的Session对象
        Session session = Session.getInstance(properties);
        // 2.开启session的debug模式，方便查看发送email的运行状态
        //session.setDebug(true);
        try {
            // 3.通过session得到transport传输对象
            Transport transport = session.getTransport();
            // 4.使用用户名密码连接上邮箱服务器,此处的密码需要到邮箱开启服务设置
            transport.connect(emailServer, sender, password);
            // 5.创建邮件
            // 创建邮件对象
            MimeMessage message = new MimeMessage(session);
            // 指明邮件的发件人
            message.setFrom(new InternetAddress(sender));
            // 指明邮件的收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
            // 邮件的标题
            message.setSubject(title);
            // 邮件的内容
            message.setContent(content, "text/html;charset=UTF-8");
            // 设置抄送人
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyReceiverAddress));
            // 6.发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            System.out.println("发送邮件出现异常");
            e.printStackTrace();
        }
    }


}
